package com.app.newcsc.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.newcsc.R;
import com.app.newcsc.utils.GlideUtils;
import com.app.newcsc.utils.SpUtils;

public class AuthorizationDialog {

    private Context mContext;
    private Dialog dialog;
    private Window window;
    private TextView tv_app;
    private TextView tv_name;
    private TextView tv_cancel;
    private TextView tv_confirm;
    private ImageView iv_img;

    public AuthorizationDialog(Context mContext) {
        this.mContext = mContext;
        initData();
    }

    private void initData() {
        dialog = new Dialog(mContext, R.style.dialog_fullscreen);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏title
        window = dialog.getWindow();
        View inflate = View.inflate(mContext, R.layout.dialog_authorization, null);
        tv_app = inflate.findViewById(R.id.tv_app);
        tv_name = inflate.findViewById(R.id.tv_name);
        tv_cancel = inflate.findViewById(R.id.tv_cancel);
        tv_confirm = inflate.findViewById(R.id.tv_confirm);
        iv_img = inflate.findViewById(R.id.iv_img);

        tv_app.setText(SpUtils.get(mContext,"csc_name") + "申请使用");
        tv_name.setText(SpUtils.get(mContext, "phone"));
//        UserEntity userEntity = ((AppApplication)AppApplication.getInstance()).getUserEntity();
//        if(userEntity != null)
//        {
//
//        }else {
//
//        }
        GlideUtils.loadToCircle(mContext,SpUtils.get(mContext,"userHead"),R.mipmap.logo).into(iv_img);

        if (window != null) {
            window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
            dialog.setContentView(inflate);
            dialog.setCanceledOnTouchOutside(true);
            window.setWindowAnimations(R.style.bottom_popup);
            window.setGravity(Gravity.BOTTOM);
        }
        tv_cancel.setOnClickListener(v -> dismiss());
    }

    public void show(View.OnClickListener onItemClickListener) {
        if (!dialog.isShowing()) {
            dialog.show();
        }
        tv_confirm.setOnClickListener(onItemClickListener);
    }

    public void cancel(View.OnClickListener onItemClickListener){
        if (!dialog.isShowing()) {
            dialog.show();
        }
        tv_cancel.setOnClickListener(onItemClickListener);
    }

    public void dismiss() {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public boolean isShowing()
    {
        if(dialog == null)
        {
            return false;
        }
        return dialog.isShowing();
    }
}
